/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.ui.chat.client;

import org.jb.notification.client.NotificationListener;
import org.jb.ui.chat.client.rpc.ChatServiceAsync;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class ChatWidget extends LayoutContainer implements NotificationListener<ChatMessage> {

	/**
	 * 
	 */
	public ChatWidget() {
		setLayout( new BorderLayout() );
		
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins( new Margins(5) );
		
		center = new LayoutContainer();
		center.setScrollMode(Scroll.AUTO);
		
		add( center, centerData );
		
		final TextField<String> text = new TextField<String>();
		BorderLayoutData southData = new BorderLayoutData(LayoutRegion.SOUTH, 20 );
		
		text.addKeyListener( new KeyListener() {
			public void componentKeyPress(ComponentEvent event) {
				if (event.getKeyCode() == 13 ) {
					final String value = text.getValue();
					if (value == null || value.isEmpty())
						return;
					text.setValue("");
					
					chatService.sendMessage( value, new AsyncCallback<Void>() {

						@Override
						public void onFailure(Throwable caught) 
						{
							Info.display("Message non transmis", value );
						}

						@Override
						public void onSuccess(Void result) {}
					} );
					
				}
			  }
		} );
		add( text, southData );
	}
	
	/**
	 * 
	 * @param messages
	 */
	public void onNotification( ChatMessage message ) {
		
		Html html = new Html();
		if (message.isSystem()) {
			html.setHtml("<b>" + message.getMessage() + "</b>" );
		} else {
			html.setHtml( "<b>" + message.getUser() + "</b> : " + message.getMessage() );
		}
		center.add( html );
		
		if (deferredCommand == null) {
			deferredCommand = new Command() {
				@Override
				public void execute() {
					center.layout();
					center.scrollIntoView( center.getItem( center.getItemCount() - 1 ) );
					deferredCommand = null;
				}
			};
			DeferredCommand.addCommand( deferredCommand );
		}
		
		
	}
	
	@Override
	public String getType() {
		return "message";
	}
		
	@Inject
	private ChatServiceAsync chatService;
	
	private LayoutContainer center;
	private Command deferredCommand = null;
	
}
